package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.service.InteractiveRecordService;
import cn.com.connext.scrm.service.NoteService;
import cn.com.connext.scrm.service.dto.InteractiveRecordDTO;
import cn.com.connext.scrm.service.dto.NoteDTO;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import cn.com.connext.scrm.web.rest.vm.*;
import com.codahale.metrics.annotation.Timed;
import com.connext.common.exception.ServiceException;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/interactiveRecord")
public class InteractiveRecordResource extends BaseResource {
    private final Logger log = LoggerFactory.getLogger(InteractiveRecordResource.class);

    @Autowired
    private InteractiveRecordService interactiveRecordService;

    @Autowired
    private NoteService noteService;

    public InteractiveRecordResource() {
        super("InteractiveRecordResource");
    }

    @Deprecated
    @GetMapping("/getMyInteraction")
    @ApiOperation(value = "获取所有互动列表", tags = {"互动列表"}, notes = "获取所有互动列表")
    @Timed
    public ResponseEntity getMyInteraction(@ApiParam Pageable pageable,
                                                                       @RequestParam String userId,
                                                                       @RequestParam Integer kind) {
        if (StringUtils.isEmpty(userId)) {
            throw new ServiceException(MyErrorCode.ERROR_USERID_EMPTY);
        }
//        if (StringUtils.isEmpty(type.toString())) {
//            throw new ServiceException(MyErrorCode.ERROR_TYPE_EMPTY);
//        }
        try {
            Page<InteractiveRecordDTO> interactiveRecordPage = interactiveRecordService.getMyInteraction(pageable, userId, kind);

            return entityResponse(interactiveRecordPage);
        } catch (Exception e) {
            log.error("NoteResource->getNoteList:{}", e);
            return null;
        }
    }

    @PostMapping("/getMyInteraction")
    @ApiOperation(value = "获取所有互动列表", tags = {"互动列表"}, notes = "获取所有互动列表")
    @Timed
    public ResponseEntity getMyInteraction(@RequestBody UserIdKindVM vm) {
        if (StringUtils.isEmpty(vm.getUserId())) {
            throw new ServiceException(MyErrorCode.ERROR_USERID_EMPTY);
        }
//        if (StringUtils.isEmpty(type.toString())) {
//            throw new ServiceException(MyErrorCode.ERROR_TYPE_EMPTY);
//        }
        try {
            Page<InteractiveRecordDTO> interactiveRecordPage = interactiveRecordService.getMyInteraction(new PageRequest(vm.getPage(), vm.getPageSize()), vm.getUserId(), vm.getKind());

            return entityResponse(interactiveRecordPage);
        } catch (Exception e) {
            log.error("NoteResource->getNoteList:{}", e);
            return null;
        }
    }

    @Deprecated
    @GetMapping("/getInteractionInfo")
    @ApiOperation(value = "查看互动详情", tags = {"互动列表"}, notes = "查看互动详情")
    @Timed
    public ResponseEntity<NoteDTO> getInteractionInfo(@RequestParam String id,
                                                      @RequestParam String noteId,
                                                      @RequestParam String userId) {
        NoteDTO noteDTO = noteService.findOneDTO(noteId, userId);

        interactiveRecordService.updateStatus(id);

        return entityResponse(Optional.ofNullable(noteDTO));
    }

    @PostMapping("/getInteractionInfo")
    @ApiOperation(value = "查看互动详情", tags = {"互动列表"}, notes = "查看互动详情")
    @Timed
    public ResponseEntity<NoteDTO> getInteractionInfo(@RequestBody InterActionInfoVM vm) {
        NoteDTO noteDTO = noteService.findOneDTO(vm.getNoteId(), vm.getUserId());

        interactiveRecordService.updateStatus(vm.getId());

        return entityResponse(Optional.ofNullable(noteDTO));
    }

    @Deprecated
    @GetMapping("/clearLikeInteraction")
    @ApiOperation(value = "清除点赞互动的红点", tags = {"互动列表"}, notes = "清除点赞互动的红点")
    @Timed
    public ResponseEntity clearLikeRedDot(@RequestParam String userId){

        interactiveRecordService.clearLikeRedDot(userId);
        return entityResponse("点赞已阅");
    }

    @PostMapping("/clearLikeInteraction")
    @ApiOperation(value = "清除点赞互动的红点", tags = {"互动列表"}, notes = "清除点赞互动的红点")
    @Timed
    public ResponseEntity clearLikeRedDot(@RequestBody UserIdVM vm){

        interactiveRecordService.clearLikeRedDot(vm.getUserId());
        return entityResponse("点赞已阅");
    }

    @Deprecated
    @GetMapping("/countUnLookup")
    @ApiOperation(value = "统计尚未查看的互动记录数", tags = {"互动列表"}, notes = "统计尚未查看的互动记录数")
    @Timed
    public ResponseEntity<Integer> countUnLookUp(@RequestParam String unionId) {
        if (StringUtils.isEmpty(unionId)) {
            throw new ServiceException(MyErrorCode.ERROR_UNIONID_EMPTY);
        }

        Integer count = interactiveRecordService.queryUnLookUpByUnionId(unionId);

        return entityResponse(count);
    }

    @PostMapping("/countUnLookup")
    @ApiOperation(value = "统计尚未查看的互动记录数", tags = {"互动列表"}, notes = "统计尚未查看的互动记录数")
    @Timed
    public ResponseEntity<Integer> countUnLookUp(@RequestBody UnionIdVM vm) {
        if (StringUtils.isEmpty(vm.getUnionId())) {
            throw new ServiceException(MyErrorCode.ERROR_UNIONID_EMPTY);
        }
        Integer count = interactiveRecordService.queryUnLookUpByUnionId(vm.getUnionId());
        return entityResponse(count);
    }

    @Deprecated
    @GetMapping("/countUnLookupWithType")
    @ApiOperation(value = "统计尚未查看的互动记录数(按照类型)", tags = {"互动列表"}, notes = "统计尚未查看的互动记录数(按照类型)")
    @Timed
    public ResponseEntity<Integer> countUnLookupWithType(@RequestParam String unionId,
                                                         @RequestParam Integer kind) {
        if (StringUtils.isEmpty(unionId)) {
            throw new ServiceException(MyErrorCode.ERROR_UNIONID_EMPTY);
        }

        Integer count = interactiveRecordService.queryUnLookUpByUnionIdAndType(unionId, kind);

        return entityResponse(count);
    }

    @PostMapping("/countUnLookupWithType")
    @ApiOperation(value = "统计尚未查看的互动记录数(按照类型)", tags = {"互动列表"}, notes = "统计尚未查看的互动记录数(按照类型)")
    @Timed
    public ResponseEntity<Integer> countUnLookupWithType(@RequestBody UnionIdKindVM vm) {
        if (StringUtils.isEmpty(vm.getUnionId())) {
            throw new ServiceException(MyErrorCode.ERROR_UNIONID_EMPTY);
        }
        Integer count = interactiveRecordService.queryUnLookUpByUnionIdAndType(vm.getUnionId(), vm.getKind());
        return entityResponse(count);
    }

    @Deprecated
    @GetMapping("/countUnLookupExceptLikes")
    @ApiOperation(value = "统计尚未查看的互动记录数(@和评论)", tags = {"互动列表"}, notes = "统计尚未查看的互动记录数(@和评论)")
    @Timed
    public ResponseEntity<Integer> countUnLookupExceptLikes(@RequestParam String unionId,
                                                         @RequestParam Integer kind) {
        if (StringUtils.isEmpty(unionId)) {
            throw new ServiceException(MyErrorCode.ERROR_UNIONID_EMPTY);
        }

        Integer count = interactiveRecordService.queryUnLookUpExceptLikes(unionId, kind);

        return entityResponse(count);
    }

    @PostMapping("/countUnLookupExceptLikes")
    @ApiOperation(value = "统计尚未查看的互动记录数(@和评论)", tags = {"互动列表"}, notes = "统计尚未查看的互动记录数(@和评论)")
    @Timed
    public ResponseEntity<Integer> countUnLookupExceptLikes(@RequestBody UnionIdKindVM vm) {
        if (StringUtils.isEmpty(vm.getUnionId())) {
            throw new ServiceException(MyErrorCode.ERROR_UNIONID_EMPTY);
        }
        Integer count = interactiveRecordService.queryUnLookUpExceptLikes(vm.getUnionId(), vm.getKind());
        return entityResponse(count);
    }
}
